package ch.fhnw.qualiscan.application.elements;

import java.util.Arrays;
import java.util.List;

import ch.fhnw.qualiscan.core.db.pojos.Answer;
import ch.fhnw.qualiscan.core.db.pojos.Question;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.OptionGroup;

@SuppressWarnings("serial")
public class QuestionForm extends HorizontalLayout implements Property.ValueChangeListener{
	
	public Question question;
	public String questionText;
	public int subquestionNumber;
	public int answer = 0;
	
	
	/**
	 * This function displays a question as a standalone question.
	 * @param question the question
	 * @param numbering number of the question
	 * @param questionText question as a string
	 */
	public QuestionForm(Answer answer, Question question, int questionNumber, int subquestionNumber, String questionText, boolean isSubquestion) {
		
		this.subquestionNumber = subquestionNumber;
		this.questionText = questionText;
		this.question = question;
		
		List<Integer> answers = Arrays.asList(new Integer[] {1,2,3,4,5});
		
		OptionGroup answerForm = new OptionGroup(null, answers);
		
		if(answer != null) {
			this.answer = answer.getAnswer();
			answerForm.setValue(answer.getAnswer());
		}
		
		setWidth("100%");
	
		Label numb;
		
		if(isSubquestion == true) {
			setMargin(true, false, true, true);
			numb = new Label(questionNumber + "." + subquestionNumber);
			setStyleName("subquestion");
			numb.setStyleName("normalNumber");
		} else {
			numb = new Label(questionNumber + "");
			setStyleName("noSubquestion");
			numb.setStyleName("bigNumber");
			setMargin(true, false, false, false);
		}
		
		numb.setWidth("36px");
		addComponent(numb);
		
		Label questionLabel = new Label(questionText);
		if (answer == null) {
			//questionLabel.setStyleName("");
		}
		addComponent(questionLabel);
		setExpandRatio(questionLabel, 1.0f);
		
		HorizontalLayout formWrap = new HorizontalLayout();
		formWrap.setSpacing(true);
		formWrap.setWidth("290px");
		formWrap.addComponent(answerForm);
		
		answerForm.setItemCaption(1, "");
		answerForm.setItemCaption(2, "");
		answerForm.setItemCaption(3, "");
		answerForm.setItemCaption(4, "");
		answerForm.setItemCaption(5, "");
		
		answerForm.setStyleName("h-optiongroup fillOutOg");
		answerForm.setImmediate(true);
		answerForm.addListener(this);
		
		addComponent(formWrap);
	}

	/**
	 * This function takes care of the event which gets fired when clicked on the form.
	 */
	public void valueChange(ValueChangeEvent event) {
		answer = (Integer) event.getProperty().getValue();
	}
	
}
